<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-20 21:41:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-07 22:37:15
 * @Descripttion: 用户订单
 */
namespace app\agent\controller\system;
use think\Db;
class UserOrder extends \app\agent\controller\Common
{
    public function index(){
        $batch = (new \app\common\model\UserOrders())->where(['agent_id' => $this->userInfo['id']])->group('batch')->order('id desc')->select();
        $this->assign('batch',$batch);
        $driver = (new \app\common\model\Driver())->where(['agent_id' => $this->userInfo['id']])->order('id desc')->select();
        $this->assign('driver',$driver);
        return view();
    }

    public function list(){
        $limit = request()->param('limit') ?? 15;
        $page = request()->param('page');
        $page = $page ? ($page - 1) * $limit : 0;
        $search = request()->param();
        $query = (new \app\common\model\UserOrders())->where(['agent_id' => $this->userInfo['id']]);
        isset($search['goods_title']) && $search['goods_title'] != "" ? $query->where("title like '%{$search['goods_title']}%'") : '';
        isset($search['batch']) && $search['batch'] != "" ? $query->where("batch like '%{$search['batch']}%'") : '';
        if(!empty($search['tpye'])){
            if($search['tpye'] == 'q'){
                $query->where([
                    'driver_q_id' => 0,
                    'driver_q_state' => 0,
                    'state' => 1,
                ]);
            }
            if($search['tpye'] == 's'){
                $query->where([
                    'driver_id' => 0,
                    'driver_state' => 0,
                    'state' => 1,
                ]);
            }
        }
        $query->order("id desc");

        $list = $query->limit($page, $limit)->append([
            'addtime_text','state_text','agent_title','bus_title',
            'driver_q_title','driver_title'
            ])->select();
        $count = $query->count();
        return self::successResult('',$list,$count);
    }

   public function info(){
        $id = request()->param('id');
        $info = (new \app\common\model\UserOrders())
                    ->where(['id' => $id])
                    ->append(['goods_info'])
                    ->append([
                        'addtime_text','state_text','driver_q_title','driver_title',
                        'driver_q_state_text','driver_state_text','bus_state_text'
                        ])
                    ->find();
   
        $this->assign("info",$info);
        return view();
   }
   public function q_add(){
     
        if (request()->isPost()) {
            $order_id = explode(",",request()->param('order_id'));
            $driver_q_id = request()->param('driver_id');
            #查询订单是否存在
            $order = (new \app\common\model\UserOrders())->whereIn('id', $order_id)->update([
                'driver_q_state' => 1,
                'driver_q_id' => $driver_q_id,
            ]);
            
            return self::successResult('成功');
        } else {
            $cate = (new \app\common\model\GoodsCate())->lists();
            $this->assign("cate",$cate);
            return view();
        }

    }
    public function s_add(){
     
        if (request()->isPost()) {
            $order_id = explode(",",request()->param('order_id'));
            $driver_id = request()->param('driver_id');
            #查询订单是否存在
            $order = (new \app\common\model\UserOrders())->whereIn('id', $order_id)->update([
                'driver_state' => 0,
                'driver_id' => $driver_id,
            ]);
            return self::successResult('成功');
        } else {
            $cate = (new \app\common\model\GoodsCate())->lists();
            $this->assign("cate",$cate);
            return view();
        }

    }
   public function goods(){
        $limit = request()->param('limit') ?? 15;
        $page = request()->param('page');
        $page = $page ? ($page - 1) * $limit : 0;
        $search = request()->param();
    
        $query = (new \app\common\model\Goods())->where(['is_del' => 0]);

        isset($search['goods_title']) && $search['goods_title'] != "" ? $query->where("title like '%{$search['goods_title']}%'") : '';
        $query->order("id desc");
        $where = [];
        if(!empty($search['bus_id'])){
            $where['bus_id'] = $search['bus_id'];
        }
        $list = $query->limit($page, $limit)->where($where)->append(['cate_title'])->select();
        $count = $query->count();
        return self::successResult('',$list,$count);
    }
        
   public function goods_sku(){
        $limit = request()->param('limit') ?? 15;
        $page = request()->param('page');
        $page = $page ? ($page - 1) * $limit : 0;
        $search = request()->param();
        $query = (new \app\common\model\GoodsSku())
                ->alias("goods_sku")
                ->join("{$this->tdb}goods goods","goods.id = goods_sku.goods_id")
                ->field([
                    'goods_sku.id',
                    'goods_sku.money',
                    'goods_sku.sku_title',
                    'goods.img',
                    'goods.title',
                    'goods.introduce',
                    'goods.cate_id',
                    ])
                ->where(['is_del' => 0]);
        isset($search['title']) && $search['title'] != "" ? $query->where("title like '%{$search['title']}%'") : '';
        $query->order("goods_sku.id desc");
        $where = [];
        if(!empty($search['bus_id'])){
            $where['bus_id'] = $search['bus_id'];
        }
        $list = $query->limit($page, $limit)->where($where)
                ->append(['cate_title'])
                ->select();
        $count = $query->count();
        return self::successResult('',$list,$count);
}

   public function delete(){
        $id = request()->param('id');
        if (empty($id)) $this->error("请选择要删除的数据");
        $table = new \app\common\model\Goods();
        if (is_array($id)) {
            $id = implode(",", $id);
            $row = $table->whereIn('id', $id)->update(['is_del' => 1]);
        } else {
            $row = $table->where('id', $id)->update(['is_del' => 1]);
        }
        return $row ? self::successResult("成功") : self::errorResult("失败");
   }
  
   public function is_index()
   {
       $id = request()->param('id');
       $is_index = request()->param('is_index');
       if (empty($id)) $this->error("请选择要修改的数据");
       $table = new \app\common\model\Goods();
       $row = $table->where('id', $id)->update(['is_index' => $is_index]);
       return $row ? self::successResult() : self::errorResult('操作失败!请刷新后重试！');
   }
 
}
